package com.cskaoyan.controller;



import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.CskaoyanmallAddress;
import com.cskaoyan.bean.vo.WxAddressVo;
import com.cskaoyan.bean.vo.WxAddressDetailBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.AddressService;
import com.cskaoyan.service.ZbRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/wx/address")
public class WxAddressController {
@Autowired
    AddressService addressService;
@Autowired
    ZbRegionService zbRegionService;

@GetMapping("/list")
    public Object list(){
    List<WxAddressVo> wxAddressVoList = addressService.queryAll();
    return BaseRespVo.success(wxAddressVoList);
}

@GetMapping("/detail")
    public Object detaile(Integer id){
    CskaoyanmallAddress address  = addressService.queryAddressById(id);
    Integer provinceId = address.getProvinceId();
    Integer cityId = address.getCityId();
    Integer areaId = address.getAreaId();
    String province = zbRegionService.queryProvinceById(provinceId);
    String city = zbRegionService.queryCityById(cityId);
    String area = zbRegionService.queryAreaById(areaId);
    WxAddressDetailBo wxAddressDetailBo = new WxAddressDetailBo(
            address.getId(),
            address.getName(),
            address.getUserId(),
            address.getProvinceId(),
            address.getCityId(),
            address.getAreaId(),
            province,
            city,
            area,
            address.getAddress(),
            address.getMobile(),
            address.getIsDefault());

    return BaseRespVo.success(wxAddressDetailBo);
}

@PostMapping("/save")
    public Object save(@RequestBody CskaoyanmallAddress cskaoyanmallAddress){
    if (0 == cskaoyanmallAddress.getId()){
        cskaoyanmallAddress.setAddTime(new Date());
        Integer i = addressService.insertAddress(cskaoyanmallAddress);
        return BaseRespVo.success(cskaoyanmallAddress.getId());
    }else {
        cskaoyanmallAddress.setUpdateTime(new Date());
        Integer i = addressService.updateAddress(cskaoyanmallAddress);
        return BaseRespVo.success(cskaoyanmallAddress.getId());
    }

}

@PostMapping("/delete")
    public Object delete(@RequestBody CskaoyanmallAddress cskaoyanmallAddress){
    Integer i = addressService.deleteAddressById(cskaoyanmallAddress);
    return BaseRespVo.success(cskaoyanmallAddress);
}
}
